//分页
//分页统一配置
window.pageC = {
	limit: 10,
	layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'], //自定义分页布局['count', 'prev', 'page', 'next', 'limit', 'skip']
	groups: 3, //只显示 5 个连续页码
	//first: true, //不显示首页
	//last: true, //不显示尾页
	prev: '<', //自定义“上一页”的内容
	next: '>', //自定义“下一页”的内容
	limits: [10, 20],
};

//局部刷新方式
window.pageAjax = function(argObj) {
	layui.use('laypage', function() {
		var laypage = layui.laypage;
		var elem = argObj.elem,
			url = argObj.url,
			curr = argObj.page || 1,
			page = argObj.page || 1,
			limit = argObj.limit || 10,
			pageName = argObj.pageName || 'page',
			limitName = argObj.limitName || 'limit',
			count = argObj.count || 'count',
			success = argObj.success,
			argData = argObj.data || {};
		var pageLimit = {};
		pageLimit[pageName + ''] = page;
		pageLimit[limitName + ''] = limit;

		$.get(url, $.extend(argData, pageLimit), function(data) {
			success && success(data, {
				curr: page,
				limit: limit
			});
			laypage.render({
				elem: elem,
				count: (typeof count === 'string') ? eval('data.' + count) : data.count || 10, //后台获取总条数
				curr: curr, //设定初始在第 1 页
				limit: limit, //每页显示一条
				layout: pageC.layout2,
				groups: pageC.groups,
				first: pageC.first,
				last: pageC.last,
				prev: pageC.prev,
				next: pageC.next,
				jump: function(obj, first) {
					//首次不执行
					if(!first) {
						//obj包含了当前分页的所有参数，比如：
						//console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
						//console.log(obj.limit); //得到每页显示的条数
						pageLimit[pageName + ''] = obj.curr;
						pageLimit[limitName + ''] = obj.limit;

						$.get(url, $.extend(argData, pageLimit), function(data) {
							success && success(data, obj);
						});
					}
				}
			});
		});
	});
}; //刷新页面
var pageUrl = function(argObj) {
	layui.use('laypage', function() {
		var laypage = layui.laypage;
		var elem = argObj.elem,
			url = argObj.url,
			hrefPage = argObj.hash,
			page = function() {
				//通过url获取当前页
				var reg = new RegExp(hrefPage + "=(\\d+)");
				var page = location.search.match(reg);
				return page ? page[1] : 1;
			}(),
			curr = page,
			limit = argObj.limit || 10,
			pageName = argObj.pageName || 'page',
			limitName = argObj.limitName || 'limit',
			count = argObj.count || 'count',
			success = argObj.success,
			argData = argObj.data || {};
		var pageLimit = {};
		pageLimit[pageName + ''] = page;
		pageLimit[limitName + ''] = limit;

		$.get(url, $.extend(argData, pageLimit), function(data) {
			success && success(data, {
				curr: page,
				limit: limit
			});
			laypage.render({
				elem: elem,
				count: (typeof count === 'string') ? eval('data.' + count) : data.count || 10, //后台获取总条数
				curr: curr, //设定初始在第 1 页
				limit: limit, //每页显示一条
				layout: pageC.layout2,
				groups: pageC.groups,
				first: pageC.first,
				last: pageC.last,
				prev: pageC.prev,
				next: pageC.next,
				jump: function(obj, first) {
					//首次不执行
					if(!first) {
						var search = location.search;
						var reg = new RegExp(hrefPage + "=(\\d+)");
						var page = reg.test(location.search);
						if(page) {
							search = search.replace(reg, hrefPage + '=' + obj.curr);
						} else if(!search) {
							search = search + '?' + hrefPage + '=' + obj.curr;
						} else {
							search = search + '&' + hrefPage + '=' + obj.curr;
						}
						location.search = search;
					}
				}
			});
		});
	});
};

//hash模式
var pageHash = function(argObj) {
	layui.use('laypage', function() {
		var laypage = layui.laypage;
		var elem = argObj.elem,
			url = argObj.url,
			limit = argObj.limit || 10,
			hrefPage = argObj.hash,
			page = location.hash.replace('#!' + hrefPage + '=', '') || 1,
			curr = page,
			pageName = argObj.pageName || 'page',
			limitName = argObj.limitName || 'limit',
			count = argObj.count || 'count',
			success = argObj.success,
			argData = argObj.data || {};
		var pageLimit = {};
		pageLimit[pageName + ''] = page;
		pageLimit[limitName + ''] = limit;

		$.get(url, $.extend(argData, pageLimit), function(data) {
			success && success(data, {
				curr: page,
				limit: limit
			});
			laypage.render({
				elem: elem,
				count: (typeof count === 'string') ? eval('data.' + count) : data.count || 10, //后台获取总条数
				curr: curr, //获取起始页
				limit: limit, //每页显示条数
				hash: hrefPage, //自定义hash值
				layout: pageC.layout2,
				groups: pageC.groups,
				first: pageC.first,
				last: pageC.last,
				prev: pageC.prev,
				next: pageC.next,
				jump: function(obj, first) {
					//首次不执行
					if(!first) {
						//obj包含了当前分页的所有参数，比如：
						//console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
						//console.log(obj.limit); //得到每页显示的条数
						pageLimit[pageName + ''] = obj.curr;
						pageLimit[limitName + ''] = obj.limit;

						$.get(url, $.extend(argData, pageLimit), function(data) {
							success && success(data, obj);
						});
					}
				}
			});
		});
	});
};